package com.tjetc.service.Impl;

import com.tjetc.dao.MerchantStatisticsMapper;
import com.tjetc.entity.DishList;
import com.tjetc.entity.view.DishTopView;
import com.tjetc.entity.view.OrdersStatisticsView;
import com.tjetc.entity.view.TurnOverView;
import com.tjetc.entity.view.UserStatisticsView;
import com.tjetc.service.MerchantStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;

@Service
public class MerchantStatisticsServiceImpl implements MerchantStatisticsService {

    @Autowired
    MerchantStatisticsMapper merchantStatisticsMapper;
    @Override
    public DishTopView getDishTop(LocalDate beginDate, LocalDate endDate,Integer merchantId) {
        LocalDateTime begin=LocalDateTime.of(beginDate, LocalTime.MIN);
        LocalDateTime end=LocalDateTime.of(endDate,LocalTime.MAX);
        //查询销量前十的菜品列表
        List<DishList> dishList=merchantStatisticsMapper.selectDishList(begin,end,merchantId);
        DishTopView dishTopView=new DishTopView();
        List dishName=new ArrayList<String>();
        List numbers=new ArrayList<Integer>();
        System.out.println(dishList);
        //将菜品和销量以及日期进行封装
        for (DishList list : dishList) {
            System.out.println(list.getDishName());
            System.out.println(list.getNumber());
            dishName.add(list.getDishName());
            numbers.add(list.getNumber());
        }
        dishTopView.setDishName(dishName);
        dishTopView.setNumber(numbers);
        return dishTopView;
    }

    @Override
    public UserStatisticsView getUserStatistics(LocalDate beginDate, LocalDate endDate, Integer merchantId) {
        List<LocalDate> dateList=new ArrayList<LocalDate>();
        //获得日期列表
        dateList.add(beginDate);
        while(!beginDate.equals(endDate)){
            beginDate=beginDate.plusDays(1);
            dateList.add(beginDate);
        }
        List<Integer> users=new ArrayList<Integer>();
        List<Integer> newUsers=new ArrayList<Integer>();

        //根据日期列表搜索该日期之前所有的用户数量以及包含该日期之后所有的用户数量，相减就是新用户数量
        for (LocalDate date : dateList) {
            HashMap map=new HashMap();
            map.put("merchantId",merchantId);
            LocalDateTime end=LocalDateTime.of(date,LocalTime.MAX);
            LocalDateTime begin=LocalDateTime.of(date,LocalTime.MIN);
            map.put("end",end);
            //今日后用户总数量
            Integer number=merchantStatisticsMapper.countUsers(map);
            map.put("end",begin);
            //今日前用户总数量
            Integer newNumber=merchantStatisticsMapper.countUsers(map);
            users.add(number);
            newUsers.add(number-newNumber);
        }
        UserStatisticsView userStatisticsView=new UserStatisticsView();
        userStatisticsView.setDateList(dateList);
        userStatisticsView.setUsers(users);
        userStatisticsView.setNewUsers(newUsers);
        return userStatisticsView;
    }

    @Override
    public OrdersStatisticsView getOrderStatistics(LocalDate beginDate, LocalDate endDate, Integer merchantId) {
        List<LocalDate> dateList=new ArrayList<LocalDate>();
        //获得日期列表
        dateList.add(beginDate);
        while(!beginDate.equals(endDate)){
            beginDate=beginDate.plusDays(1);
            dateList.add(beginDate);
        }
        List<Integer> success=new ArrayList<>();
        List<Integer> unSuccess=new ArrayList<>();
        List<Integer> total=new ArrayList<>();
        //根据日期查询每一日的订单数量
        for (LocalDate date : dateList) {
            LocalDateTime begin=LocalDateTime.of(date,LocalTime.MIN);
            LocalDateTime end=LocalDateTime.of(date,LocalTime.MAX);
            HashMap map=new HashMap<>();
            map.put("merchantId",merchantId);
            map.put("begin",begin);
            map.put("end",end);
            //完成的订单数量
            Integer totalNumber=merchantStatisticsMapper.countOrders(map);
            total.add(totalNumber);
            map.put("beginStatus",2);
            Integer successNumber= merchantStatisticsMapper.countOrders(map);
            map.remove("beginStatus",2);
            //未完成的订单的数量
            map.put("endStatus",0);
            Integer unSuccessNumber=merchantStatisticsMapper.countOrders(map);
            success.add(successNumber);
            unSuccess.add(unSuccessNumber);
        }
        OrdersStatisticsView ordersStatisticsView=new OrdersStatisticsView();
        ordersStatisticsView.setDateList(dateList);
        ordersStatisticsView.setTotal(total);
        ordersStatisticsView.setSuccess(success);
        ordersStatisticsView.setUnSuccess(unSuccess);
        return ordersStatisticsView;
    }

    @Override
    public TurnOverView getTurnOver(LocalDate beginDate, LocalDate endDate, Integer merchantId) {
        List<BigDecimal> turnOver=new ArrayList<>();
        List<LocalDate> dateList=new ArrayList<>();
        TurnOverView turnOverView=new TurnOverView();
        //获得日期列表
        dateList.add(beginDate);
        while (!beginDate.equals(endDate)){
            beginDate=beginDate.plusDays(1);
            dateList.add(beginDate);
        }
        //遍历日期列表搜索每一天的营业额
        for (LocalDate date : dateList) {
            HashMap hashMap=new HashMap<>();
            LocalDateTime begin=LocalDateTime.of(date,LocalTime.MIN);
            LocalDateTime end=LocalDateTime.of(date,LocalTime.MAX);
            hashMap.put("merchantId",merchantId);
            hashMap.put("begin",begin);
            hashMap.put("end",end);
            BigDecimal amount=merchantStatisticsMapper.getSumAmount(hashMap);
            if(amount==null) {
                amount= BigDecimal.valueOf(0);
            }
            turnOver.add(amount);
        }
        turnOverView.setAmountList(turnOver);
        turnOverView.setDateList(dateList);
        return turnOverView;
    }


}
